﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common.DataLayer;

namespace Common.Tools
{
    public class Sablon
    {
        public void Kreiraj(int brojSablona, int id)
        {

            #region Uzimamo sql iz tabele Sablon za dati id
            CommonDataContext dc = new CommonDataContext();

            string sql = dc.Sablons.SingleOrDefault(c => c.SablonId == brojSablona).Upit;
            string word = dc.Sablons.SingleOrDefault(c => c.SablonId == brojSablona).Word;

            sql = String.Format(sql, id);
            #endregion

            Object fileName = Common.Tools.Parametar.Citaj("Sablon", "FolderSaSablonima") + word;
            Object readOnly = true;
            Object isVisible = false;
            Object missing = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
            wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

            SqlConnection conn = new SqlConnection(Common.Properties.Settings.Default.ConnectionString);


            if (wordDoc.Bookmarks.Exists("logo"))
            {
                Object range = wordDoc.Bookmarks["logo"].Range;
                //string imagePath = @"C:\DigiSkola\DigiSkola\Sabloni\Logo.png";
                string imagePath = Common.Tools.Parametar.Citaj("Sablon", "LogoSablona");
                object tr = true;
                object fa = false;
                wordDoc.InlineShapes.AddPicture(imagePath, ref tr, ref fa, ref range);
            }

            try
            {

                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];
                foreach (DataRow row in dt.Rows)
                {
                    foreach (DataColumn col in dt.Columns)
                    {
                        foreach (Microsoft.Office.Interop.Word.Bookmark b in wordDoc.Bookmarks)
                        {
                            if (String.Equals(b.Name, col.ColumnName))
                                b.Range.Text = row[col].ToString();
                        }
                    }
                }

                Common.Tools.MessageInformation.Show("Dokument je uspešno upisan u delovodnu knjigu.");

            }
            catch (Exception e)
            {
                MessageError.Show("Error: " + e);
            }
            finally
            {
                conn.Close();
            }

            object FileName = Common.Tools.Parametar.Citaj("Sablon", "FolderZasnimanje") + "test.docx";
            object LockComments = false;
            object AddToRecentFiles = false;
            object ReadOnlyRecommended = false;
            object EmbedTrueTypeFonts = false;
            object SaveNativePictureFormat = true;
            object SaveFormsData = false;
            object SaveAsAOCELetter = false;

            wordDoc.SaveAs(
                ref FileName,
                null,
                ref LockComments,
                ref missing,
                ref AddToRecentFiles,
                ref missing,
                ref ReadOnlyRecommended,
                ref EmbedTrueTypeFonts,
                ref SaveNativePictureFormat,
                ref SaveFormsData,
                ref SaveAsAOCELetter);


            wordDoc.SaveAs();
            wordApp.Visible = true;

            
        }

        public Sablon()
        {
        }
    }
}
